package com.sunshine.lnuplus.ui.activity

import android.Manifest
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.app.ActivityCompat
import androidx.lifecycle.ViewModelProvider
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.utils.CalenderUtils
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.utils.TagHelper
import com.sunshine.lnuplus.view.DialogWithView
import com.sunshine.lnuplus.viewModel.CalenderViewModel
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_calender.*

class CalenderActivity : BaseActivity() {

    private lateinit var viewModel: CalenderViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_calender)

        viewModel = ViewModelProvider(this).get(CalenderViewModel::class.java)
        if(checkCallingOrSelfPermission("android.permission.READ_CALENDAR") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.WRITE_CALENDAR") != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.READ_CALENDAR,
                    Manifest.permission.WRITE_CALENDAR
                ),
                TagHelper.CALENDER
            )
        }
        initView()
    }
    private fun initView() {
        var time = 0
        StatusBarUtils.setStatusBarWithColor(this, color)
        arcView_calender.setColor(color, color)
        title = "导出到日历"
        button_time.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_calender_time, null, false)
            val dialog = DialogWithView.newInstance(
                view,
                "请输入提前提醒分钟",
                false,
                DialogInterface.OnClickListener { _, _ ->
                    try {
                        time = view.findViewById<AppCompatEditText>(R.id.edit_calender_time).text.toString().toInt()
                        button_time.text = "将在上课或倒计时开始前${time}分钟提醒"
                    }catch (e: Exception){
                        Toasty.error(this, "输入有问题呀~").show()
                    }

            },
                null)
            dialog.show(supportFragmentManager, "calender")
        }
        button_to_calender.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null, false)
            view.findViewById<TextView>(R.id.progress_text).text = "正在添加课表到日历"
            val loading = DialogWithView.newInstance(view, "正在导出",false, null, null)
            loading.show(supportFragmentManager, "add_calender")
            object : Thread() {
                override fun run() {
                    CalenderUtils.initLesson(this@CalenderActivity, time, loading)
                }
            }.start()
        }
        button_delete.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null, false)
            view.findViewById<TextView>(R.id.progress_text).text = "正在从日历删除课表"
            val loading = DialogWithView.newInstance(view, "正在删除",false, null, null)
            loading.show(supportFragmentManager, "delete_calender")
            object : Thread() {
                override fun run() {
                    CalenderUtils.deleteCalendarEvent(this@CalenderActivity, loading, "辽大+课表")
                }
            }.start()
        }

        button_countdown_to_calender.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null, false)
            view.findViewById<TextView>(R.id.progress_text).text = "正在添加倒计时到日历"
            val loading = DialogWithView.newInstance(view, "正在导出，请注意只会导出有时间的倒计时噢",false, null, null)
            loading.show(supportFragmentManager, "add_calender")
            object : Thread() {
                override fun run() {
                    CalenderUtils.initCountDown(this@CalenderActivity, time, loading, viewModel.getAllCountDown())
                }
            }.start()
        }
        button_countdown_delete.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null, false)
            view.findViewById<TextView>(R.id.progress_text).text = "正在从日历删除倒计时"
            val loading = DialogWithView.newInstance(view, "正在删除",false, null, null)
            loading.show(supportFragmentManager, "delete_calender")
            object : Thread() {
                override fun run() {
                    CalenderUtils.deleteCalendarEvent(this@CalenderActivity, loading, "辽大+倒计时")
                }
            }.start()
        }

    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            TagHelper.CALENDER ->if(grantResults.isNotEmpty()){
                var count = 0
                grantResults.forEach {
                    if(it == PackageManager.PERMISSION_GRANTED){
                        count++
                    }
                }
                if(count != grantResults.size){
                    Toasty.error(this, "未授予日历读写权限").show()
                    finish()
                }
            }
        }
    }
}
